package o;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import com.huawei.hms.framework.network.restclient.hianalytics.HianalyticsData;
import com.huawei.hms.support.api.entity.hwid.HwIDConstant.ACTION;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.hwid.core.datatype.UserInfo;
import com.sina.weibo.sdk.auth.AccessTokenKeeper;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.auth.WbAuthListener;
import com.sina.weibo.sdk.auth.WbConnectErrorMessage;

public class fnw extends fny {
    private boolean ˎ = false;
    private Context ॱ;

    public fnw(fnv o_fnv, Context context, foc o_foc) {
        super(o_fnv, o_foc);
        this.ॱ = context;
    }

    @TargetApi(24)
    public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest webResourceRequest) {
        fnm.ˊ("AuthWebViewClient", "shouldOverrideUrlLoading,request.getUrl()");
        return ॱ(webView, webResourceRequest.getUrl().toString());
    }

    public boolean shouldOverrideUrlLoading(WebView webView, String str) {
        fnm.ˊ("AuthWebViewClient", "shouldOverrideUrlLoading,url");
        return ॱ(webView, str);
    }

    private boolean ॱ(WebView webView, String str) {
        if (str.startsWith("sms:")) {
            try {
                Intent intent = new Intent(ACTION.HWID_SCHEME_URL);
                intent.putExtra(UserInfo.ADDRESS, str.replace("sms:", ""));
                intent.setType("vnd.android-dir/mms-sms");
                this.ॱ.startActivity(intent);
                return true;
            } catch (Exception e) {
            }
        } else if (!str.startsWith("sinaweibo://browser/close")) {
            if (ˊ(str) && !TextUtils.isEmpty(fnr.ˋ(str).getString("access_token"))) {
                return true;
            }
            return false;
        } else if (this.ˋ.ˋ() == null || TextUtils.isEmpty(this.ˋ.ˋ().ˎ())) {
            return true;
        } else {
            String ˎ = this.ˋ.ˋ().ˎ();
            fnu ˎ2 = fnu.ˎ();
            if (ˎ2.ˋ(ˎ) != null) {
                ˎ2.ˋ(ˎ).cancel();
            }
            ˎ2.ॱ(ˎ);
            return true;
        }
    }

    private boolean ˊ(String str) {
        Uri parse = Uri.parse(this.ˋ.ˋ().ˏ().getRedirectUrl());
        Uri parse2 = Uri.parse(str);
        Object host = parse.getHost();
        return !TextUtils.isEmpty(host) && host.equals(parse2.getHost());
    }

    public void onPageStarted(WebView webView, String str, Bitmap bitmap) {
        fnm.ˊ("AuthWebViewClient", "onPageStarted:");
        if (this.ˊ != null) {
            this.ˊ.ˊ(webView, str, bitmap);
        }
        super.onPageStarted(webView, str, bitmap);
    }

    public void onPageFinished(WebView webView, String str) {
        fnm.ˊ("AuthWebViewClient", "onPageFinished:");
        super.onPageFinished(webView, str);
        if (this.ˊ != null) {
            this.ˊ.ˊ(webView, str);
        }
        if (ˊ(str) && !this.ˎ) {
            this.ˎ = true;
            ˎ(str);
            webView.stopLoading();
            if (this.ˊ != null) {
                this.ˊ.ॱ();
            }
        }
    }

    @TargetApi(24)
    public void onReceivedError(WebView webView, WebResourceRequest webResourceRequest, WebResourceError webResourceError) {
        super.onReceivedError(webView, webResourceRequest, webResourceError);
        fnm.ˊ("AuthWebViewClient", "onReceivedError");
        if (this.ˊ != null) {
            this.ˊ.ॱ(webView, webResourceError.getErrorCode(), webResourceError.getDescription().toString(), webResourceRequest.getUrl().toString());
        }
    }

    public void onReceivedError(WebView webView, int i, String str, String str2) {
        super.onReceivedError(webView, i, str, str2);
        fnm.ˊ("AuthWebViewClient", "onReceivedError");
        if (this.ˊ != null) {
            this.ˊ.ॱ(webView, i, str, str2);
        }
    }

    private void ˎ(String str) {
        Bundle ˋ = fnr.ˋ(str);
        String string = ˋ.getString(HwAccountConstants.EXTRA_OPLOG_ERROR);
        String string2 = ˋ.getString(HianalyticsData.ERROR_CODE);
        String string3 = ˋ.getString("error_description");
        WbAuthListener wbAuthListener = null;
        if (!(this.ˋ.ˋ() == null || TextUtils.isEmpty(this.ˋ.ˋ().ˎ()))) {
            String ˎ = this.ˋ.ˋ().ˎ();
            fnu ˎ2 = fnu.ˎ();
            wbAuthListener = ˎ2.ˋ(ˎ);
            ˎ2.ॱ(ˎ);
        }
        if (string == null && string2 == null) {
            if (wbAuthListener != null) {
                Oauth2AccessToken parseAccessToken = Oauth2AccessToken.parseAccessToken(ˋ);
                AccessTokenKeeper.writeAccessToken(this.ॱ, parseAccessToken);
                wbAuthListener.onSuccess(parseAccessToken);
            }
        } else if (wbAuthListener != null) {
            wbAuthListener.onFailure(new WbConnectErrorMessage(string2, string3));
        }
    }

    public void ˏ() {
        super.ˏ();
        if (this.ˋ.ˋ() != null && !TextUtils.isEmpty(this.ˋ.ˋ().ˎ())) {
            String ˎ = this.ˋ.ˋ().ˎ();
            fnu ˎ2 = fnu.ˎ();
            if (ˎ2.ˋ(ˎ) != null) {
                ˎ2.ˋ(ˎ).cancel();
            }
            ˎ2.ॱ(ˎ);
        }
    }

    public boolean ॱ() {
        ˏ();
        if (this.ˊ != null) {
            this.ˊ.ॱ();
        }
        return true;
    }
}
